import 'package:flutter/material.dart';
//import 'package:flutter/cupertino.dart';
import 'package:english_words/english_words.dart';

class ChatMessageScreen extends StatelessWidget {
		@override
		Widget build(BuildContext context) {
				return Scaffold(
						appBar: AppBar(
								title: Text('聊天窗口'),
								elevation: Theme.of(context).platform == TargetPlatform.iOS ? 0.0 : 4.0,
						),
						body: ChatMessageBuilder());
		}
}

class ChatMessageBuilder extends StatefulWidget {
		@override
		_ChatMessageBuilderState createState() => _ChatMessageBuilderState();
}

class _ChatMessageBuilderState extends State<ChatMessageBuilder>
		with TickerProviderStateMixin {
		final TextEditingController _controller = TextEditingController();
		final List<ChatMessage> _messages = <ChatMessage>[];

		void _handleSubmit(String text) {
				if(text.trim().length == 0 ) {
						return;
				}
				_controller.clear();
				String name = WordPair
						.random()
						.asPascalCase;
				ChatMessage message = ChatMessage(
						text: text,
						name: name,
						animationController: AnimationController(
								vsync: this, duration: Duration(milliseconds: 500)),
				);
				setState(() {
						_messages.insert(0, message);
				});
				message.animationController.forward();
		}


		Widget _buildTextComposer() {
				return new Container(
						margin: EdgeInsets.symmetric(horizontal: 8.0),
						child: Row(
								children: <Widget>[
										Flexible(
												child: new TextField(
														controller: _controller,
														onSubmitted: _handleSubmit,
														decoration: InputDecoration.collapsed(
																hintText: '请输入消息'),
												),
										),
										Container(
												margin: EdgeInsets.symmetric(horizontal: 4.0),
												child: IconButton(
														icon: Icon(Icons.send),
														onPressed: () => _handleSubmit(_controller.text)))
								],
						));

				@override
				dispose() {
						for (ChatMessage message in _messages) {
								message.animationController.dispose();
						}
						super.dispose();
				}
		}

		@override
		Widget build(BuildContext context) {
				return Column(
						children: <Widget>[
								Flexible(
										child: ListView.builder(
												padding: EdgeInsets.all(8.0),
												reverse: true,
												itemBuilder: (_, index) => _messages[index],
												itemCount: _messages.length,
										),
								),
								Divider(
										height: 1.0,
								),
								Container(
										decoration: BoxDecoration(color: Theme
												.of(context)
												.cardColor),
										child: _buildTextComposer())
						],
				);
		}
}

class ChatMessage extends StatelessWidget {
		final String text;
		final String name;
		final AnimationController animationController; //new

		ChatMessage({this.text, this.name, this.animationController});

		@override
		Widget build(BuildContext context) {
				return SizeTransition(
						sizeFactor:
						CurvedAnimation(parent: animationController, curve: Curves.easeOut),
						child: Container(
								margin: EdgeInsets.symmetric(vertical: 10.0),
								child: Row(
										crossAxisAlignment: CrossAxisAlignment.start,
										children: <Widget>[
												Container(
														margin: EdgeInsets.only(right: 16.0),
														child: CircleAvatar(child: Text(name[0]))),
												// 支持换行，否则文字太长无法显示
												Expanded(
														child: Column(
																crossAxisAlignment: CrossAxisAlignment.start,
																children: <Widget>[
																		new Text(name, style: Theme
																				.of(context)
																				.textTheme
																				.subhead),
																		Container(
																				margin: EdgeInsets.only(top: 5.0),
																				child: Text(text))
																],
														),
												)
										],
								)),
				);
		}
}
